<?php

namespace app\controller;

use app\BaseController;
use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;

class Course extends BaseController
{

    // http://www.penyou.com/course/cats
    public function cats()
    {
        $lists = Db::name('goods_cats')->field('id,title,route')->order('ord ASC')->where('is_delete = 0 AND is_show = 1')->select();

        singleJson($lists);
    }

    public function lists()
    {
        $params = Request::param();

        $page = 'goods';

        $catName = getParam($params, 'catName');

        $cat_id = getSingleField('goods_cats', 'route', $catName, 'id');

        $sql_where = "A.is_delete = 0";

        if ($cat_id > 0) {
            $sql_where .= " AND A.cat_id = " . $cat_id;
        }


        $sql_order = getSqlOrder("A." . getParam($params, 'ordField'), getParam($params, 'ordOrder'), "A.id DESC");

        $pageSize = getParam($params, 'pageSize', 8);

        $currentPage = getParam($params, 'currentPage', 1);

        $limit_start = $pageSize * ($currentPage - 1);

        $limit_end = $pageSize;

        $limit = getParam($params, 'limit');

        if ($limit) {
            $limit_start = 0;
            $limit_end = 100;
        }

        $sql_field = 'A.title,A.id,A.price,A.cover';
        $lists = Db::name($page)->alias('A')->field($sql_field)
            ->where($sql_where)->order($sql_order)->limit($limit_start, $limit_end)->select()->toArray();

        //    echo Db::name($page)->getLastSql();
        $count = Db::name($page)->alias('A')->where($sql_where)->count();

        listsJson($lists, $count);
    }

    public function detail()
    {
        $id = Request::param('id');
        //  $info = Db::name('goods')->field('title,cover,content,price,addtime,cat_id')->where(array('id' => $id,'is_delete'=>0))->find();
        $info = Db::name('goods')->alias('A')->field('A.title,A.cover,A.content,A.price,A.addtime,A.cat_id,B.title as cat_name,B.route')->leftJoin('kt_goods_cats B', 'B.id = A.cat_id')->where(array('A.id' => $id, 'A.is_delete' => 0))->find();

        singleJson($info, '该商品不存在');
    }

// http://www.penyou.com/course/chapters
    public function chapters()
    {
        // 获取所有章节


        $id = Request::param('id');
        $chapters = Db::name('chapters')->field('id,title,description')->where(array('gid' => $id, 'is_delete' => 0, 'is_onsale' => 1))->order("ord ASC")->select()->toArray();

        foreach ($chapters as $v) {
            $chapterIds[] = $v['id'];
        }
        $sql_where = "1=1 ";
        if ($chapterIds) {
            $sql_where .= " AND A.chapter_id IN (" . implode(',', $chapterIds) . ")";
        }
        $userId = getUserId();
        $is_vip = getIsVip($userId);
        $sql_field = "";
        if($is_vip){
            $sql_field  =",B.url";
        }
        $materials = Db::name('chapters_materials')->field("A.chapter_id,B.title,B.id,B.type,B.is_try,B.content_try,B.content,B.cover,B.duration".$sql_field)->alias('A')->leftJoin('kt_material B', 'B.id = A.material_id')->where($sql_where)->order("A.ord ASC")->select()->toArray();

        foreach ($chapters as $k => $v) {
            foreach ($materials as $k2 => $v2) {
                if ($v['id'] === $v2['chapter_id']) {
                    $chapters[$k]['materials'][] = $v2;
                }

            }

        }

//        print_r($chapters);
        singleJson($chapters, '该商品不存在');
    }

    public function latest()
    {
        $lists = Db::name('goods')->alias('A')->field('A.id,A.title,A.cover,A.price,A.addtime,A.cat_id,B.title as cat_name,B.route')->leftJoin('kt_goods_cats B', 'B.id = A.cat_id')->order('A.id DESC')->where(array('A.is_delete' => 0))->limit(10)->select();

        singleJson($lists, '最新商品获取失败');
    }
}
